package com.aispeech.aicube.voice.observer;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.aispeech.aicube.CubeApplication;
import com.aispeech.aicube.robot.MyBrain;
import com.aispeech.aicube.robot.MyVoice;
import com.aispeech.dui.dds.DDS;
import com.aispeech.dui.dsk.duiwidget.CommandObserver;

/**
 * 客户端CommandObserver, 用于处理客户端动作的执行以及快捷唤醒中的命令响应.
 * 例如在平台配置客户端动作： command://call?phone=$phone$&name=#name#,
 * 那么在CommandObserver的onCall方法中会回调topic为"call", data为
 */
public class DuiCommandObserver implements CommandObserver {
    private String TAG = "DuiCommandObserver";

    private final String COMMAND_CUBE_WK_CMD_START = MyVoice.CUBE_WK_CMD_START[4];
    private final String COMMAND_CUBE_WK_CMD_START2 = MyVoice.CUBE_WK_CMD_START2[4];
    private final String COMMAND_CUBE_WK_CMD_REVISE = MyVoice.CUBE_WK_CMD_REVISE[4];
    private final String COMMAND_CUBE_WK_CMD_WAIT = MyVoice.CUBE_WK_CMD_WAIT[4];

    private MyVoice.VoiceListener mListener;

    public DuiCommandObserver() {
    }

    // 注册当前更新消息
    public void regist(MyVoice.VoiceListener listener) {
        mListener = listener;
        DDS.getInstance().getAgent().subscribe(new String[]{
                COMMAND_CUBE_WK_CMD_START,
                COMMAND_CUBE_WK_CMD_START2,
                COMMAND_CUBE_WK_CMD_REVISE,
                COMMAND_CUBE_WK_CMD_WAIT
        }, this);
    }

    // 注销当前更新消息
    public void unregist() {
        DDS.getInstance().getAgent().unSubscribe(this);
    }

    @Override
    public void onCall(String command, String data) {
        Log.d(TAG, "command: " + command + "  data: " + data);
        try {
            if (null != mListener) {
                if (MyBrain.BRAIN_STATE.INTERACTING.equals(MyBrain.getInstance().getState())
                        || MyBrain.BRAIN_STATE.READY_SHOW.equals(MyBrain.getInstance().getState())
                        || MyBrain.BRAIN_STATE.IDLE.equals(MyBrain.getInstance().getState())) {
                    if (COMMAND_CUBE_WK_CMD_START.equals(command) || COMMAND_CUBE_WK_CMD_START2.equals(command)) {
                        mListener.onVoiceCMD(MyBrain.BRAIN_EVENT.VOICE_EVENT_SHOW);
                    } else if (COMMAND_CUBE_WK_CMD_REVISE.equals(command)) {
                        mListener.onVoiceCMD(MyBrain.BRAIN_EVENT.VOICE_EVENT_REVISE);
                    } else if (COMMAND_CUBE_WK_CMD_WAIT.equals(command)) {
                        mListener.onVoiceCMD(MyBrain.BRAIN_EVENT.VOICE_EVENT_WAIT);
                    }
                }
            } else {
                Log.e(TAG, "onCall: not register listener");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 拨打电话
    private void phoneDial(String number) {
        if (number == null) {
            return;
        }
        Log.e(TAG, "phoneDial:" + number);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("tel:" + number));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        CubeApplication.getContext().startActivity(intent);
    }

}
